﻿using KAs.SqlSugar;
using Microsoft.Extensions.Configuration;
using SqlSugar;

namespace Microsoft.Extensions.DependencyInjection;

public static class SqlSugarSetupExtensions
{
    /// <summary>
    /// 添加sqlsugar服务
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    public static void AddSqlSugarSetup(this IServiceCollection services, IConfiguration configuration)
    {
        // 注入sqlsuger服务
        services.AddScoped<ISqlSugarClient>(sp =>
                new SqlSugarClient(
                    new ConnectionConfig()
                    {
                        // 注意使用数据库类型
                        //DbType = DbType.SqlServer,
                        DbType = DbType.MySql,
                        ConnectionString = configuration.GetConnectionString("Default"),
                        IsAutoCloseConnection = true,
                    }
                )
            );
        // 注入自定义公共仓储
        services.AddScoped(typeof(IRepository<,>), typeof(Repository<,>));
    }
}
